<?php

class Adept_Util_Serializer
{

    protected $subject;
    protected $serialized;
    protected $classPaths = array();

    function __construct($subject)
    {
        $this->subject = $subject;
    }

    function getSubject()
    {
        if ($this->serialized) {
            $this->_includeFiles();
            $this->subject = unserialize($this->serialized);
            $this->serialized = null;
        }
        return $this->subject;
    }

    function getClassPaths()
    {
        return $this->classPaths;
    }

    function __sleep()
    {
        if(is_null($this->serialized)) {
            $this->serialized = serialize($this->subject);
            $this->_fillClassPathInfo($this->serialized);
        }
        return array('serialized', 'classPaths');
    }

    function _includeFiles()
    {
        foreach($this->classPaths as $path) {
            require_once($path);
        }
    }

    function _fillClassPathInfo($serialized)
    {
        $classes = self :: extractSerializedClasses($serialized);
        $this->class_paths = array();

        foreach($classes as $class) {
            $reflect = new ReflectionClass($class);
            $this->classPaths[] = $reflect->getFileName();
        }
    }

    static function extractSerializedClasses($str)
    {
        $extract_class_names_regexp = '~([\||;]O|^O):\d+:"([^"]+)":\d+:\{~';
        if(preg_match_all($extract_class_names_regexp, $str, $m)){
            return array_unique($m[2]);
        } else {
            return array();
        }
    }

}